 /*      SClassObject.cpp
 *
 *      Copyright 2009 virpool <virpool@ukrpost.ua>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "SClassObject.h"
#include "SGarbageCollector.h"

SClassObject::SClassObject(string name): SObject(name, "Object")
{
	isGCable = false;
}

SClassObject::~SClassObject()
{

}

int SClassObject::AddField(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || args == 0)
		return S_INVOKE_INCORRECT;
	list<s_param>::iterator p = args->begin();
	if (p->type != S_TYPE)
	{
		return S_INVOKE_INCORRECT;
	}
	//switch ((*(S_ARG_TYPES*)p->val))
	//{
	//case S_OBJECT:
		p++;
		if (p == args->end() || p->type != S_STRING)
		{
			return S_INVOKE_INCORRECT;
		}
		string objName = ((string*)p->val)->c_str();
		p++;
		if (p == args->end() || p->type != S_OBJECT)
		{
			return S_INVOKE_INCORRECT;
		}
		((SClassObject*)obj)->fields.Add(objName, (SObject*)p->val);
		if (!((SClassObject*)obj)->isGCable)
		{
			((SClassObject*)obj)->isGCable = true;
			SGarbageCollector::AddToCache(&((SClassObject*)obj)->fields);
		}
	//	break;
	//default:
	//	return S_INVOKE_INCORRECT;
	//}
	return S_INVOKE_OK;
}

int SClassObject::RemoveField(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || res == NULL || args == 0)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_STRING)
	{
		return S_INVOKE_INCORRECT;
	}
	((SClassObject*)obj)->fields.Remove(*((string*)p->val));
	return S_INVOKE_OK;
}

int SClassObject::GetField(SObject* obj, s_param* res, param_list* args)
{
	if (obj == NULL || res == NULL || args == 0 || args->size() == 0)
		return S_INVOKE_INCORRECT;
	param_list::iterator p = args->begin();
	if (p->type != S_STRING)
	{
		return S_INVOKE_INCORRECT;
	}
	res->val = ((SClassObject*)obj)->fields.Get(*((string*)p->val));
	res->type = S_OBJECT;
	return S_INVOKE_OK;
}
